package com.century.sjt.window;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.century.sjt.R;
import com.century.sjt.adapter.AddressAdapter;
import com.century.sjt.entity.Address;

import java.util.List;

/**
 * Created by asus on 2016/8/30.
 */
public class AddressWindow extends PopupWindow {
    private Context mContext;
    private View mMenuView;
    //翻译监听
    private AddressWindowListItemClickListener  listener;
    private ListView listView;
    private AddressAdapter adapter;
    private TextView tvTitle;
    private List<Address> DataList;

    public List<Address> getDataList() {
        return DataList;
    }

    public void setDataList(List<Address> dataList) {
        DataList = dataList;
    }

    public AddressWindow(Context context, final List<Address> mlist, RequestQueue mQueue) {
        super(context);
        this.mContext = context;
        this.DataList = mlist;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.sjt_address_window_item, null);
        listView = (ListView) mMenuView.findViewById(R.id.lv_listview);
        tvTitle = (TextView) mMenuView.findViewById(R.id.tv_title);
        //设置按钮监听

        //设置PayPopupWindow的View
        this.setContentView(mMenuView);
        //设置PayPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置PayPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PayPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PayPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置PayPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        adapter = new AddressAdapter(mContext, DataList,mQueue);


        listView.setAdapter(adapter);
        // 设置适配器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                dismiss();
                listener.onItemClick(DataList.get(i));

            }
        });
    }
    public void setAddressWindowListItemClickListener(AddressWindowListItemClickListener listener) {
        this.listener = listener;
    }
    public interface AddressWindowListItemClickListener{
        /**
         * 支付选择事件监听
         * @param message
         * @return
         */
        void onItemClick(Address message);
        void onDismissClick();
    }
}
